﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OnTheWay.Interfaces;
using OnTheWay.Models;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;

namespace OnTheWay.Domain
{
    public class DomainCommon<T, TIService> : DomainTransfer<T, TIService>, IObjectBehaviors<T, Guid>
    {
        protected IsoDateTimeConverter timeConverter = ServiceTransfer.mDateTimeConverter;

        public bool Insert(Guid authid, T model)
        {
            return this.InvokeTransfer<bool>("Insert", new object[] { 
                JsonConvert.SerializeObject(authid), 
                JsonConvert.SerializeObject(model,ServiceTransfer.mDateTimeConverter) 
            });
        }

        public bool Update(Guid authid, T model)
        {
            return this.InvokeTransfer<bool>("Update", new object[] { 
                JsonConvert.SerializeObject(authid), 
                JsonConvert.SerializeObject(model,ServiceTransfer.mDateTimeConverter) 
            });
        }

        public bool Delete(Guid authid, T model)
        {
            return this.InvokeTransfer<bool>("Delete", new object[] { 
                JsonConvert.SerializeObject(authid), 
                JsonConvert.SerializeObject(model,ServiceTransfer.mDateTimeConverter) 
            });
        }

        public T GetByID(Guid authid, Guid id)
        {
            return this.InvokeTransfer<T>("GetByID", new object[] { 
                JsonConvert.SerializeObject(authid), 
                JsonConvert.SerializeObject(id) 
            });
        }

        public bool UpdateList(Guid authid, IList<T> list)
        {
            return this.InvokeTransfer<bool>("UpdateList", new object[] { 
                JsonConvert.SerializeObject(authid), 
                JsonConvert.SerializeObject(list,ServiceTransfer.mDateTimeConverter)  
            });
        }

        public bool Exists(Guid authid, Guid id)
        {
            return this.InvokeTransfer<bool>("Exists", new object[] { 
                JsonConvert.SerializeObject(authid), 
                JsonConvert.SerializeObject(id) });
        }

        public bool DeleteList(Guid authid, IList<T> list)
        {
            return this.InvokeTransfer<bool>("DeleteList", new object[] { 
                JsonConvert.SerializeObject(authid), 
                JsonConvert.SerializeObject(list,ServiceTransfer.mDateTimeConverter)  
            });
        }

        public bool DeleteByID(Guid authid, Guid id)
        {
            return this.InvokeTransfer<bool>("DeleteByID", new object[] { 
                JsonConvert.SerializeObject(authid), 
                JsonConvert.SerializeObject(id) 
            });
        }

        public long GetRecordCount(Guid authid, string where)
        {
            return this.InvokeTransfer<long>("GetRecordCount", new object[] { 
                JsonConvert.SerializeObject(authid), where 
            });
        }

        public IList<T> GetListByPage(Guid authid, string where, Page page)
        {
            return this.InvokeTransferList("GetListByPage", new object[] { 
                JsonConvert.SerializeObject(authid), where,  
                JsonConvert.SerializeObject(page)
            });
        }

        public IList<T> GetListWhere(Guid authid, string where, string order, string asc)
        {
            return this.InvokeTransferList("GetListWhere", new object[] { 
                JsonConvert.SerializeObject(authid), where, order, asc 
            });
        }

        public IList<T> GetInValidData(Guid authid)
        {
            return this.InvokeTransferList("GetInValidData", new object[] { 
                JsonConvert.SerializeObject(authid) 
            });
        }

        public IList<T> GetInValidDataPage(Guid authid, Page page)
        {
            return this.InvokeTransferList("GetInValidDataPage", new object[] { 
                JsonConvert.SerializeObject(authid), 
                JsonConvert.SerializeObject(page)
            });
        }
    }
}
